import csv
import matplotlib.pyplot as plt
from collections import OrderedDict
import matplotlib as mpl
import matplotlib.font_manager as font_manager
import string


# font and pattern
plt.rcParams["font.family"] = "Times New Roman"
csfont = {'fontname':'Times New Roman'}
font = font_manager.FontProperties(family='Times New Roman', size=25)
colors = ['k','blue','cyan','gray', 'red', \
           'blue',  'cyan', 'lightcoral', 'turquoise','red','blue','green','pink']
markers = ['s','D','^','o','*','s','+','x','X','D','^','<','>','v'] 
sizes = [7, 7, 7, 7, 7, 3, 4, 3, 3, 3, 3, 3, 6,5,4,3,2,2]
linestyles = OrderedDict(
    [('solid',               (0, ())),
     ('dashdotted',          (0, (3, 3, 1, 3))),
     ('dashdotdotted',       (0, (3, 2, 1, 2, 1, 2))),
     ('dashed',              (0, (3, 3))),
     ('dotted',              (0, (1, 3))),
     ('dashed',              (0, (3, 3))),
     ('loosely dotted',      (0, (1, 10))),
     ('densely dotted',      (0, (1, 1))),
     ('loosely dashed',      (0, (5, 10))),
     ('densely dashed',      (0, (5, 1))),
     ('loosely dashdotted',  (0, (3, 10, 1, 10))),
     ('densely dashdotted',  (0, (3, 1, 1, 1))),
     ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
     ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))])


#Caption text
options1 = ["Best Observed Track",\
            r"$\mathit{Clz}_{\mathrm{YSU-1}}=0.0001$",\
            r"$\mathit{Clz}_{\mathrm{YSU-1}}=0.01$",\
            r"$\mathit{Clz}_{\mathrm{YSU-1}}=1$",\
            r"$\mathit{Clz}_{\mathrm{YSU-1}}=100$"]

#hurricane to plot
hurricanes = ["Gert"]
  
  
# subplot positions 
position2 = [[0,4,0,7]]

#hurricane extracted data
dir_wi1 = ['##########.csv']



#########################################
# Plot normalized intensity time series #
#########################################


fig = plt.figure(figsize=(22,30))
spec = mpl.gridspec.GridSpec(ncols=23, nrows=29)

for kk in range(len(hurricanes)):
    
    c=0
    rows=[]
    Times=[]
    Times=[]
    values=[]
    with open(dir_wi1[kk], mode='r') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        line_count = 0
        for row in csv_reader:
            if line_count == 0:
                print(f'Column names are {", ".join(row)}')
                Times.append(list(row.keys()))
                line_count += 1
            #print(row)
            rows.append(row)
            values.append(list(row.values()))
            line_count += 1
        print(f'Processed {line_count} lines.')
    
    Times0=Times[0]
    
    
    ax = fig.add_subplot(spec[position2[kk][0]:position2[kk][1],\
                              position2[kk][2]:position2[kk][3]])
    ax.text(0.05, 0.85, '('+string.ascii_lowercase[kk]+')', transform=ax.transAxes, 
            size=30, **csfont)


    for i in range(0,line_count-1):
        if i==23:
            tmp=[float(i)*0.5144444 for i in values[i]]
            #tmp=[float(i) for i in values[i]]
        # elif (i!=2 and i!=3):
        else:
            tmp=[float(i) for i in values[i]]
        # else:
        #     continue
        
        plt.plot( Times0[:], tmp[:], color = colors[c], \
                      linestyle=list(linestyles.values())[c],\
                      linewidth=5, markersize=sizes[c])
        plt.xticks(fontsize=25, **csfont)
        plt.yticks(fontsize=25, **csfont)
        plt.ylim([10, 80])

        c+=1
        
    for axis in ['top','bottom','left','right']:
            ax.spines[axis].set_linewidth(2)
    ax.tick_params(length=5, width=2)
    ax.legend(options1, bbox_to_anchor=(1.3, 0.95), prop=font, \
                frameon=False)
    
    plt.ylabel(r'Intensity (m/s)', **csfont, fontsize=35)
    plt.xlabel(r"Time Series (hr)", fontsize=30, **csfont)
    plt.title(hurricanes[kk]+r' (YSU)', {'size': 30}, **csfont)
    
    
    

plt.show()








